package kr.nexmore.slmsample2.view;

import java.io.IOException;
import java.net.URL;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import kr.nexmore.slmsample2.uri.Bundleclass_Uri;
import kr.nexmore.slmsample2.uri.Part_Uri;

import org.apache.log4j.PropertyConfigurator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class LeftTab implements SelectionListener {

	@Inject
	private EPartService partService;

	private Composite composite;

	public LeftTab() {
		try {
			URL installURL = Platform.getBundle("kr.nexmore.slmsample2").getEntry("/");
			String installPath = Platform.asLocalURL(installURL).getFile();
			PropertyConfigurator.configure(installPath + "/log4j.properties");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * Create contents of the view part.
	 */
	@PostConstruct
	public void createControls(Composite parent) {
		composite = parent;

		/*
		 * LoginDialog dialog = new LoginDialog(parent.getShell());
		 * dialog.open();
		 */

		parent.setLayout(new FormLayout());

		Button btnNewButton = new Button(parent, SWT.NONE);
		btnNewButton.addSelectionListener(this);

		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(0, 36);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\uB9AC\uC2A4\uD2B8");

		Button button = new Button(parent, SWT.NONE);
		button.addSelectionListener(this);

		fd_btnNewButton.left = new FormAttachment(0, 67);
		button.setText("\uCCB4\uD06C\uBC15\uC2A4");
		FormData fd_button = new FormData();
		fd_button.left = new FormAttachment(btnNewButton, -6, SWT.LEFT);
		fd_button.top = new FormAttachment(btnNewButton, 38);
		button.setLayoutData(fd_button);

		Button button_1 = new Button(parent, SWT.NONE);
		button_1.setText("통계");
		button_1.addSelectionListener(this);
		FormData fd_button_1 = new FormData();
		fd_button_1.left = new FormAttachment(btnNewButton, 6, SWT.LEFT);
		fd_button_1.top = new FormAttachment(button, 47);
		button_1.setLayoutData(fd_button_1);

		Button button_2 = new Button(parent, SWT.NONE);
		button_2.addSelectionListener(this);
		button_2.setText("그래프");
		FormData fd_button_2 = new FormData();
		fd_button_2.left = new FormAttachment(btnNewButton, 0, SWT.LEFT);
		fd_button_2.top = new FormAttachment(button_1, 50);
		button_2.setLayoutData(fd_button_2);

		Button button_3 = new Button(parent, SWT.NONE);
		button_3.setText("게시판");
		button_3.addSelectionListener(this);
		FormData fd_button_3 = new FormData();
		fd_button_3.top = new FormAttachment(button_2, 46);
		fd_button_3.right = new FormAttachment(btnNewButton, 0, SWT.RIGHT);
		button_3.setLayoutData(fd_button_3);

		Button button_4 = new Button(parent, SWT.NONE);
		button_4.setText("디렉토리");
		FormData fd_button_4 = new FormData();
		fd_button_4.top = new FormAttachment(button_3, 36);
		fd_button_4.left = new FormAttachment(button, 0, SWT.LEFT);
		button_4.setLayoutData(fd_button_4);
		button_4.addSelectionListener(this);

	}

	@PreDestroy
	public void dispose() {
	}

	@Focus
	public void setFocus() {
		// TODO Set the focus to control
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		// TODO Auto-generated method stub

		Button btn = (Button) e.widget;
		MPart part = partService.findPart(Part_Uri.PART_SECOND);

		switch (btn.getText()) {
		case "리스트":

			partService.hidePart(part);
			part.setContributionURI(Bundleclass_Uri.LISTPART);
			partService.activate(part);

			break;

		case "체크박스":
			partService.hidePart(part);
			part.setContributionURI(Bundleclass_Uri.CHECKPART);
			partService.activate(part);

			break;
		case "통계":
			partService.hidePart(part);
			part.setContributionURI(Bundleclass_Uri.CHARTPART);
			partService.activate(part);

			break;
		case "그래프":
			partService.hidePart(part);
			part.setContributionURI(Bundleclass_Uri.GRAPHPART);
			partService.activate(part);
			break;
		case "게시판":
			partService.hidePart(part);
			part.setContributionURI(Bundleclass_Uri.BOARDPART);
			partService.activate(part);

			break;
		case "디렉토리":
			partService.hidePart(part);
			part.setContributionURI(Bundleclass_Uri.DIRECTORYPART);
			partService.activate(part);
			/*
			 * DirectoryPart directoryPart = new DirectoryPart();
			 * directoryPart.open(composite.getDisplay());
			 */

			break;

		default:
			break;
		}

		// part.setVisible(true);

		/*
		 * partService.requestActivation();
		 */
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub

	}

}
